/**
	@author: Trương Hoang Linh
	@Class: ResultScene
	@input: - resultText: Kết quả của ô chữ vừa rồi
		   - congratulationText: ex: Thiên Tài, Tuyệt vời, etc...
	@ouput: Sắp xếp ô chữ kết quả, hiển thị confgratulationText 

	@ContinueButton: nhảy sang màn tiếp theo
	
	??: Tăng kim cương, kiểm tra số lượng màn hiện tại, cập nhật lại level cho người dùng, tải thêm câu hoỉ
*/

//Example Data
var resultText = "CAM CAN NAY MUC";
var congratulationTextExample = "Thiên Tài";


var WIDTH_OF_BOX = 45;
var OFFSET_BETWEEN_BOX = 2;
var MAX_LENGTH = 700;
var Y_FIRST_ROW = 195;

var instance_result;
(function (window){
	function ResultScene (gameWidth, gameHeight) {
		this.gameHeight = gameHeight;
		this.gameWidth = gameWidth; 

		this.flagBtnContinue = false;
		this.flagBtnContinueScale = 0;




		
		this.flagAction = true;

	}

	ResultScene.prototype = new createjs.Container();
	ResultScene.prototype.Container_initialize = ResultScene.prototype.initialize();
	// hàm khởi tạo
	ResultScene.prototype.initialize = function(){
		instance_result = this;
		//Initial all of resources for this class
		this.mainImg 					= this.load(contentmanager.bg_main, 400, 240);
		this.btnContinueImg 			= this.load(contentmanager.btn_continue, 649, 413, 0.8, 0.8);
		this.congratulationFrame 		= this.load(contentmanager.frame_congratulation, 400, 50, 0.6, .6);	

		//non-important text
		this.CreateText("Bạn đã tìm ra đáp án", this.congratulationFrame.x, this.congratulationFrame.y + 50, 20, '#ddd');

		// if this variable's value is true. that mean this class is acting.  
		this.SetActionForButton();
	}


	ResultScene.prototype.SetActionForButton = function() {
		var self = this;
		this.btnContinueImg.addEventListener("mousedown", self.mouseDownButtonPlay);
		this.btnContinueImg.addEventListener("mouseover", self.mouseOverButtonPlay);
		this.btnContinueImg.addEventListener("mouseout", self.mouseOutButtonPlay);
	}

	/*ResultScene.prototype.SetTextResult = function() {

	}*/

	ResultScene.prototype.load = function (data, x, y, scaleX, scaleY) {
		var bitmap = new createjs.Bitmap(data);
		var width = bitmap.image.width;
		var height = bitmap.image.height;
		bitmap.x = x || 0;
		bitmap.y = y || 0;
		bitmap.regX = width/2;
		bitmap.regY = height/2;
		bitmap.scaleX = scaleX || 1;
		bitmap.scaleY = scaleY || 1;
		bitmap.width = width*bitmap.scaleX;
		bitmap.height = height*bitmap.scaleY;
		this.addChild(bitmap);
		return bitmap;
	}



	ResultScene.prototype.CreateText = function(text, x, y, size, color) {
		var size  = size  || 30;
		var color = color || '#FFF';
		var text = new Text(text,'bold '+size+'px Arial', color);
		text.alpha = 0.8;
		text.x = x;
		text.y = y;
		text.textAlign = 'center';
		this.addChild(text);
		return text;
	}



	ResultScene.prototype.SetTextResult = function() {

		this.resultText = qanda.getAnswer();

		this.congratulationText = /*_congratulationText || */congratulationTextExample;

		//Kiem tra dau vao
		if (this.resultText == null || this.resultText.length == 0) {
			console.log("Khong co gia tri dau vao!");
			return;
		}


		this.CreateText(this.congratulationText, this.congratulationFrame.x, this.congratulationFrame.y - 13);

		//Analyze resultText 
		this.ResultTextAnalyze();
		//Next sang cau hoi tiep theo
		qanda.nextToCurrent();
				console.log("Level cua nguoi choi: " + window.level);
	}

	/*
	 return: - a number of letter of result text
	 		 - width and coordinate in order to display result
	*/
	ResultScene.prototype.ResultTextAnalyze = function() {

			var arr_resultText 	= this.resultText.split(" ");
			var arr_length 		= arr_resultText.length;

			
			var numberWordInFirstRow 	= this.HowManyWordInFirstRow(arr_resultText, arr_length);
			var pxForFirstRow = this.HowManyPxForRow(arr_resultText, 0, numberWordInFirstRow);

			this.DisplayCell(arr_resultText, 0, numberWordInFirstRow, pxForFirstRow, Y_FIRST_ROW);

			var numberWordInSecondRow 	= arr_resultText.length - numberWordInFirstRow;
			
			if (numberWordInSecondRow > 0) {
				var pxForSecondRow = this.HowManyPxForRow(arr_resultText, numberWordInFirstRow, numberWordInSecondRow);
				this.DisplayCell(arr_resultText, numberWordInFirstRow, numberWordInSecondRow, pxForSecondRow, Y_FIRST_ROW + 55);
			}




			//if ( (this.resultText.length + 1) * 45
	}


	/* How many word in the first row?
	*/
	ResultScene.prototype.HowManyWordInFirstRow = function(arr_resultText, arr_length) {
			var numberWordInFirstRow = 0;
			var temp_width = 0;


			for (var i = 0; i < arr_length; i++) {
				var length_word = arr_resultText[i].length - 1;

				var increase_length = ( (length_word++)*OFFSET_BETWEEN_BOX + parseInt(length_word * WIDTH_OF_BOX) ); 

				if ( (temp_width + increase_length) < MAX_LENGTH ) {
					temp_width += increase_length;
					numberWordInFirstRow++;
				} else {
					break;
				} 


				if ( i != (arr_length - 1) ) {
					temp_width += OFFSET_BETWEEN_BOX*2 + WIDTH_OF_BOX;
				}

			}
		 return numberWordInFirstRow;
	}

	/**
		How many px for width of row
	*/
	ResultScene.prototype.HowManyPxForRow = function(arr_resultText, start, numberWord) {

		var px = 0;

		var end = start + numberWord;

		//So luong chu cai o hang nay (co tinh hai o trong)
		var numberOfLetter = numberWord - 1;

		//Cong hai o trong ngan cach giua cac tu
		px += (numberWord - 1) * WIDTH_OF_BOX; 

		for (var i = start; i < end; i++) {
			var length_word = arr_resultText[i].length;
			console.log(arr_resultText[i]);
			numberOfLetter += length_word;

			px += parseInt(length_word * WIDTH_OF_BOX);
		}

		//Cong voi khoang trong giua moi tu
		px += parseInt( (numberOfLetter - 1) * OFFSET_BETWEEN_BOX );

		return px;
	}

	/*
		Display cell with text
	*/
	ResultScene.prototype.DisplayCell = function(arr_resultText, start, numberWord, px, y_row) {
		var X_ROW = Math.round(this.gameWidth/2 - parseInt(px/2) + parseInt(WIDTH_OF_BOX/2));
		var Y_ROW = y_row;

		var end = start + numberWord;
		var offset = 0;
		var x_cell = X_ROW;
					//voi ket qua la bao cao => can 8 o
		//this.itemCell		= this.load(contentmanager.item_answer, X_ROW, Y_ROW);

		for (var i = start; i < end; i++) {
			var word = arr_resultText[i];
			var length = word.length;
			for (var j = 0; j < length; j++) {
				this.load(contentmanager.item_answer, x_cell, Y_ROW, 0, 0);
				
				var text = this.CreateText(word[j], x_cell, Y_ROW- 15 , 35); text.visible = false;
				createjs.Tween.get(text).to({alpha:0, visible: true}).to({alpha:1}, 800);
				x_cell += WIDTH_OF_BOX + OFFSET_BETWEEN_BOX;
			}
			x_cell += WIDTH_OF_BOX;
		}
	}

	ResultScene.prototype.mouseDownButtonPlay = function() {
		if(!window.isReady){
			alert("Câu hỏi chưa sẵn sàng");
			return;
		}
		console.log(isReady);
		console.log("clicked");
		stage.removeChild(resultscene);
		resultscene.removeAllChildren();
		ingame.initialize();
		stage.addChild(ingame);
	};

	ResultScene.prototype.mouseOverButtonPlay = function() {
		var self = instance_result;
		createjs.Tween.get(self.btnContinueImg).to({scaleX: self.btnContinueImg.scaleX + 0.1,scaleY:self.btnContinueImg.scaleY + 0.1},200);
	};


	ResultScene.prototype.mouseOutButtonPlay = function() {
		var self = instance_result;
		createjs.Tween.get(self.btnContinueImg).to({scaleX: self.btnContinueImg.scaleX - 0.1,scaleY:self.btnContinueImg.scaleY - 0.1},100);	
	};


	window.ResultScene = ResultScene;
}(window));
